package yhao.infra.feature.qiniu.oss.core.request;

import com.qiniu.util.StringMap;
import yhao.infra.feature.qiniu.oss.core.BucketInfo;
import yhao.infra.feature.qiniu.oss.core.QiniuBaseRequest;
import yhao.infra.feature.qiniu.oss.core.response.QiniuBaseResponse;

/**
 * 删除空间中的文件
 * @see <a href="https://developer.qiniu.com/kodo/sdk/1239/java#rs-delete">更多明细</a>
 */
public class QiniuTokenRequest extends QiniuBaseRequest<QiniuBaseResponse> {
    //上传到加密桶还是文件桶，true为加密桶
    private boolean waterMark;
    //覆盖上传用到的key
    private String overrideKey;
    //失效时间，默认1小时
    private long expireSeconds = 60*60;

    private StringMap putPolicy = null;

    public QiniuTokenRequest(boolean waterMark) {
        this.waterMark = waterMark;
    }

    public String getOverrideKey() {
        return overrideKey;
    }

    public void setOverrideKey(String overrideKey) {
        this.overrideKey = overrideKey;
    }

    public StringMap getPutPolicy() {
        return putPolicy;
    }

    public void setPutPolicy(StringMap putPolicy) {
        this.putPolicy = putPolicy;
    }

    public boolean isWaterMark() {
        return waterMark;
    }

    public long getExpireSeconds() {
        return expireSeconds;
    }

    public void setExpireSeconds(long expireSeconds) {
        this.expireSeconds = expireSeconds;
    }

    @Override
    protected QiniuBaseResponse doRequest() {
        BucketInfo bucket = this.isWaterMark()?protectBucket:fileBucket;
        String upToken = bucket.getAuth().uploadToken(bucket.getName(), overrideKey,expireSeconds,putPolicy);

        try {
            return new QiniuBaseResponse(upToken);
        } catch (Exception ex) {
            throw new RuntimeException(ex.getMessage());
        }
    }
}
